package tarena.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springside.modules.security.utils.Digests;
import org.springside.modules.utils.Clock;
import org.springside.modules.utils.Encodes;
import tarena.comm.MyPage;
import tarena.dao.TabAccountDao;
import tarena.entity.TabAccount;

import java.util.List;
import java.util.UUID;

/**
 * 
 * 
 * @author Leslie
 */
// Spring Service Bean的标识.
@Component
@Slf4j
public class TabAccountService {

	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;

	@Autowired
	private TabAccountDao tabAccountDao;

    private Clock clock = Clock.DEFAULT;

	public TabAccount getById(Long id) {
		return tabAccountDao.getById(id);
	}

	public List<TabAccount> getAll() {
		return tabAccountDao.getAll();
	}

	/**
	 * 分页查询
	 * 
	 * @param tabAccount
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	public MyPage<TabAccount> searchPage(int currentPage, int pageSize) {
		MyPage<TabAccount> myPage = new MyPage<TabAccount>();

		Long count = tabAccountDao.searchCount();

		int pageStart = (currentPage - 1) < 0 ? 0 : (currentPage - 1) * pageSize;
		List<TabAccount> list = tabAccountDao.searchPage( pageStart, pageSize);

		int pages;
		if (count % pageSize == 0) {
		pages = new Long(count).intValue() / new Long(pageSize).intValue();
		} else {
			pages = new Long(count).intValue() / new Long(pageSize).intValue() + 1;
		}
		myPage.setSumPage(pages);
		myPage.setCurrentPage(new Long(currentPage));
		myPage.setCount(count);
		myPage.setContent(list);

		return myPage;
	}

	public MyPage<TabAccount> searchPageByName(String key, int currentPage, int pageSize) {
		MyPage<TabAccount> myPage = new MyPage<TabAccount>();

		Long count = tabAccountDao.searchCountByName(key);

		int pageStart = (currentPage - 1) < 0 ? 0 : (currentPage - 1) * pageSize;
		List<TabAccount> list = tabAccountDao.searchPageByName( key,pageStart, pageSize);

		int pages;
		if (count % pageSize == 0) {
			pages = new Long(count).intValue() / new Long(pageSize).intValue();
		} else {
			pages = new Long(count).intValue() / new Long(pageSize).intValue() + 1;
		}
		myPage.setSumPage(pages);
		myPage.setCurrentPage(new Long(currentPage));
		myPage.setCount(count);
		myPage.setContent(list);

		return myPage;
	}

	public void save(TabAccount tabAccount) {
		entryptPassword(tabAccount);
		tabAccount.setDeleted(1);
		tabAccount.setCreateDate(clock.getCurrentDate());
		tabAccount.setUid(UUID.randomUUID().toString());
		tabAccountDao.save(tabAccount);

	}


	public void update(TabAccount tabAccount) {
		tabAccount.setUpdateDate(clock.getCurrentDate());
		entryptPassword(tabAccount);
		tabAccountDao.update(tabAccount);
	}

	/**
	 * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
	 */
	private void entryptPassword(TabAccount tabAccount) {
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		tabAccount.setSalt(Encodes.encodeHex(salt));
		byte[] hashPassword = Digests.sha1(tabAccount.getPlainPassword().getBytes(), salt, HASH_INTERATIONS);
		tabAccount.setPassword(Encodes.encodeHex(hashPassword));
	}

	/**
	 * 软删除
	 */
	public void delete(Long id) {
		tabAccountDao.delete(id);
	}

	public TabAccount findUserByLoginName(String loginName) {
		log.info(loginName);
		return tabAccountDao.getByLoginName(loginName);
	}
}
